<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理 - 控制面板</title>
    <link rel="stylesheet" href="/css/style.css">
    <style>
        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            object-fit: cover;
            border: 2px solid var(--primary-color);
        }
        .user-role {
            display: inline-block;
            padding: 3px 8px;
            border-radius: 4px;
            font-size: 12px;
            color: white;
        }
        .role-admin {
            background-color: #e74c3c;
        }
        .role-user {
            background-color: #3498db;
        }
        .role-editor {
            background-color: #2ecc71;
        }
        .search-box {
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="dashboard-container">
        <div class="sidebar">
            <div class="sidebar-header">
                <h2>控制面板</h2>
            </div>
            <ul class="sidebar-menu">
                <li><a href="/dashboard">主页</a></li>
                <li><a href="/dashboard/profile">个人信息</a></li>
                <li><a href="/dashboard/change-password">修改密码</a></li>
                <li><a href="/dashboard/documents">文档管理</a></li>
                <li><a href="/dashboard/categories">分类管理</a></li>
                <li><a href="/dashboard/weblinks">网站链接管理</a></li>
                <li class="active"><a href="/dashboard/users">用户管理</a></li>
                <li><a href="/dashboard/settings">网站设置</a></li>
                <li><a href="/danmaku-display">弹幕墙</a></li>
                <li><a href="/weblinks" target="_blank">网站导航</a></li>
                <li><a href="/" target="_blank">访问首页</a></li>
                <li><a href="/logout">退出登录</a></li>
            </ul>
        </div>
        
        <div class="main-content">
            <div class="dashboard-header">
                <div class="dashboard-title">
                    <h1>用户管理</h1>
                </div>
            </div>
            
            <div class="alert alert-success" th:if="${successMessage}">
                <p th:text="${successMessage}"></p>
            </div>
            
            <div class="alert alert-danger" th:if="${errorMessage}">
                <p th:text="${errorMessage}"></p>
            </div>
            
            <div class="card">
                <div class="card-header">
                    <h2>添加新用户</h2>
                </div>
                <div class="card-body">
                    <form th:action="@{/dashboard/users/create}" method="post" th:object="${user}">
                        <div class="form-group">
                            <label for="username">用户名</label>
                            <input type="text" id="username" name="username" th:field="*{username}" class="form-control" required>
                        </div>
                        <div class="form-group">
                            <label for="email">电子邮箱</label>
                            <input type="email" id="email" name="email" th:field="*{email}" class="form-control" required>
                        </div>
                        <div class="form-group">
                            <label for="password">密码</label>
                            <input type="password" id="password" name="password" th:field="*{password}" class="form-control" required>
                            <small class="form-text text-muted">密码需要至少8个字符，包含大小写字母和数字。</small>
                        </div>
                        <div class="form-group">
                            <label for="fullName">姓名</label>
                            <input type="text" id="fullName" name="fullName" th:field="*{fullName}" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="role">角色</label>
                            <select id="role" name="role" th:field="*{role}" class="form-control" required>
                                <option value="USER">普通用户</option>
                                <option value="EDITOR">编辑</option>
                                <option value="ADMIN">管理员</option>
                            </select>
                        </div>
                        <div class="form-actions">
                            <button type="submit" class="btn btn-primary">添加用户</button>
                        </div>
                    </form>
                </div>
            </div>
            
            <div class="card mt-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h2>用户列表</h2>
                    <div class="search-box">
                        <form th:action="@{/dashboard/users}" method="get" class="d-flex">
                            <input type="text" name="search" th:value="${search}" class="form-control" placeholder="搜索用户...">
                            <button type="submit" class="btn btn-primary ml-2">搜索</button>
                        </form>
                    </div>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>头像</th>
                                    <th>用户名</th>
                                    <th>姓名</th>
                                    <th>邮箱</th>
                                    <th>角色</th>
                                    <th>注册时间</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr th:each="user : ${users}">
                                    <td th:text="${user.id}"></td>
                                    <td>
                                        <img th:src="${user.profileImage != null ? user.profileImage : '/images/default-avatar.svg'}" 
                                             alt="用户头像" class="user-avatar">
                                    </td>
                                    <td th:text="${user.username}"></td>
                                    <td th:text="${user.fullName}"></td>
                                    <td th:text="${user.email}"></td>
                                    <td>
                                        <span th:class="'user-role ' + 'role-' + ${#strings.toLowerCase(user.role)}" 
                                              th:text="${user.role == 'ADMIN' ? '管理员' : (user.role == 'EDITOR' ? '编辑' : '用户')}">
                                        </span>
                                    </td>
                                    <td th:text="${#temporals.format(user.createdAt, 'yyyy-MM-dd HH:mm')}"></td>
                                    <td>
                                        <span th:class="${user.enabled ? 'text-success' : 'text-danger'}" 
                                              th:text="${user.enabled ? '已激活' : '已禁用'}">
                                        </span>
                                    </td>
                                    <td>
                                        <a th:href="@{/dashboard/users/edit/{id}(id=${user.id})}" class="btn btn-sm btn-primary">编辑</a>
                                        <a th:if="${user.enabled}" th:href="@{/dashboard/users/disable/{id}(id=${user.id})}" 
                                           class="btn btn-sm btn-warning" 
                                           onclick="return confirm('确定要禁用此用户吗？')">禁用</a>
                                        <a th:unless="${user.enabled}" th:href="@{/dashboard/users/enable/{id}(id=${user.id})}" 
                                           class="btn btn-sm btn-success">启用</a>
                                        <a th:href="@{/dashboard/users/delete/{id}(id=${user.id})}" 
                                           class="btn btn-sm btn-danger" 
                                           onclick="return confirm('确定要删除这个用户吗？此操作不可恢复！')">删除</a>
                                        <a th:href="@{/dashboard/users/reset-password/{id}(id=${user.id})}" 
                                           class="btn btn-sm btn-info" 
                                           onclick="return confirm('确定要重置此用户的密码吗？')">重置密码</a>
                                    </td>
                                </tr>
                                <tr th:if="${users.empty}">
                                    <td colspan="9" class="text-center">没有用户记录</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script src="/js/script.js"></script>
</body>
</html> 